package cn.iocoder.yudao.module.call.mq.consumer.follow;

import cn.iocoder.yudao.module.call.enums.CallSourceEnum;
import cn.iocoder.yudao.module.call.mq.message.follow.CallFollowMessage;
import cn.iocoder.yudao.module.crm.api.follow.CrmFollowRecordApi;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.ent.api.company.EntCompanyApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 针对 {@link CallFollowMessage} 的消费者
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class CallFollowConsumer {


    @Resource
    private EntCompanyApi entCompanyApi;

    @Resource
    private CrmFollowRecordApi crmFollowRecordApi;


    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CallFollowMessage message) {
//        log.info("[CallFollowConsumer][消息内容({})]", message);
        if (CallSourceEnum.isEnt(message.getType())) {
            // 1. ent
            entCompanyApi.addEntCallFollowRecord(message.getOwnerUserId(), message.getSourceId(), message.getNumber(), message.getCallLogId(), message.isCallStatus(), message.getCallRecordUrl(), message.getPhoneName());
        } else if (CallSourceEnum.isCustomer(message.getType())) {
            // 2. customer
            crmFollowRecordApi.addCrmCallFollowRecord(CrmBizTypeEnum.CRM_CUSTOMER, message.getOwnerUserId(), Long.parseLong(message.getSourceId()), message.getCallLogId(), message.isCallStatus(), message.getCallRecordUrl(), message.getPhoneName());
        } else if (CallSourceEnum.isClue(message.getType())) {
            // 3. clue
            crmFollowRecordApi.addCrmCallFollowRecord(CrmBizTypeEnum.CRM_CLUE, message.getOwnerUserId(), Long.parseLong(message.getSourceId()), message.getCallLogId(), message.isCallStatus(), message.getCallRecordUrl(), message.getPhoneName());
        }
    }

}
